<?php
namespace App\Http\Controllers\AppApi\Msg;

use App\Services\Main\MsgSysService;
use App\Services\Main\MsgTalkService;
use App\Repositories\Admin\CompanyRepository;
use Illuminate\Http\Request;

class TalkController extends BaseController
{
    /**
     * 消息会话
     * 制作者：九哥
     */

    protected $service;
    protected $system;
    protected $com;

    public function __construct()
    {
        parent::__construct();
        $this->service = new MsgTalkService();
        $this->system = new MsgSysService();
        $this->com = new CompanyRepository();
    }

    /**
     * 主页新消息提醒
     */
    public function getIndex(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getNewTips($uid);
        if (!$model) { return $this->toJson(-3); }
        return $this->toJson(0,$model);
    }

    /**
     * 新增招聘信息窗口
     */
    public function setAdd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
//        $talkType = $request->talkType; //1工人发起会话，2企业发起会话
        $uid2 = $request->uid2;
        $jobId = $request->job;
        if (!$uid2) { return $this->toJson(-2); }
        $model = $this->service->getAppJobShow($uid,$uid2);
        if (!$model) {
            $own_new = 0;
            $other_new = 1;
            $talkArr = array(
                'own' => $uid,
                'other' => $uid2,
                'genre' => 2, //招聘消息
                'own_new' => $own_new,
                'other_new' => $other_new,
            );
            $lastId = $this->service->setAdd($talkArr);
        } else {
            $lastId = $model->id;
            $own_new = $model->own_new;
            $other_new = $model->other_new;
        }
        $company = $this->com->getShowOneByUid($uid);
        if (!$company) {
            $company = $this->com->getShowOneByUid($uid2);
        }
        $cname = $company ? $company->com_name : '';
        $dataArr = array(
            'talkId' => $lastId,
            'own' => $uid,
            'other' => $uid2,
            'own_new' => $own_new,
            'other_new' => $other_new,
            'jobId' => $jobId,
            'otherName' => $cname,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 商务消息列表
     */
    public function getCompanys(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getComTalks($uid);
        if (!$model) { return $this->toJson(-3); }
        return $this->toJson(0,$model);
    }

    /**
     * 招聘消息列表
     */
    public function getJobs(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getJobTalks($uid,$type);
        if (!$model) { return $this->toJson(-3); }
        return $this->toJson(0,$model);
    }

    /**
     * 系统消息列表
     */
    public function getSystems(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $dataArr = $this->system->getIndex();
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 系统消息详情
     */
    public function getSysShow(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $sysId = $request->sysId;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $dataArr = $this->system->getOne($sysId);
        $this->system->setReadModify($sysId,$uid); //设置已读状态
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }
}
